package ossRW

import (
	"errors"
	"github.com/gofiber/fiber/v2/utils"
	"strings"
)

func UploadImage(imageData *[]byte, imageOriginName string, savePath string) (path string, err error) {
	if len(*imageData) < 30 {
		return "", errors.New("上传文件过小")
	}
	if strings.HasSuffix(imageOriginName, "jpg") || strings.HasSuffix(imageOriginName, "png") {

		randName := utils.UUIDv4()
		randName = strings.ReplaceAll(randName, "-", "")
		if strings.HasSuffix(imageOriginName, "jpg") {
			randName += ".jpg"
		} else {
			randName += ".png"
		}
		//date := time.Now()
		//uploadPathPrefix := "upload/" + strconv.Itoa(date.Year()) + "/" + date.Month().String() + "/" +
		//	strconv.Itoa(date.Day()) + "/"
		randName = savePath + randName

		err = OssUploadByteData(imageData, randName)
		if err != nil {
			return "", err
		}
		return randName, nil
	}
	return "", errors.New("只能接受jpg或者png图片")
}
